<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item
        label="姓名"
        prop="useName"
      >
        <el-input
          v-model="queryParams.useName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="手机号"
        prop="phone"
      >
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="年级"
        prop="grade"
      >
        <el-input
          v-model="queryParams.grade"
          placeholder="请输入年级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="省"
        prop="province"
      >
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="市"
        prop="city"
      >
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="区/县"
        prop="district"
      >
        <el-input
          v-model="queryParams.district"
          placeholder="请输入区/县"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="详细地址"
        prop="address"
      >
        <el-input
          v-model="queryParams.address"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="门牌号"
        prop="doorNumber"
      >
        <el-input
          v-model="queryParams.doorNumber"
          placeholder="请输入门牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="科目阶段" prop="subjectStage">
        <el-input
          v-model="queryParams.subjectStage"
          placeholder="请输入科目阶段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item
        label="科目名称"
        prop="subject"
      >
        <el-input
          v-model="queryParams.subject"
          placeholder="请输入科目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="授课频率"
        prop="frequency"
      >
        <el-input
          v-model="queryParams.frequency"
          placeholder="请输入授课频率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="课酬"
        prop="courseFee"
      >
        <el-input
          v-model="queryParams.courseFee"
          placeholder="请输入课酬"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="提交时间"
        prop="createdAt"
      >
        <el-date-picker
          clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择提交时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-if="isTutorrequest"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-if="isTutorrequest"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-if="isTutorrequest"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tutorrequestList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        :selectable="isSelectable"
      />
      <el-table-column
        label="姓名"
        align="center"
        prop="useName"
      />
      <el-table-column
        label="手机号"
        width="150"
        align="center"
        prop="phone"
      />
      <el-table-column
        label="性别"
        align="center"
        prop="studentGender"
      />
      <el-table-column
        label="年级"
        align="center"
        width="130"
        prop="grade"
      />
      <el-table-column
        label="省"
        align="center"
        prop="province"
      />
      <el-table-column
        label="市"
        align="center"
        prop="city"
      />
      <el-table-column
        label="区/县"
        align="center"
        prop="district"
      />
      <el-table-column
        label="详细地址"
        align="center"
        width="120"
        prop="address"
      />
      <el-table-column
        label="门牌号"
        align="center"
        prop="doorNumber"
      />
      <!-- <el-table-column label="科目阶段" align="center" prop="subjectStage" /> -->
      <el-table-column
        label="科目名称"
        align="center"
        prop="subject"
      />
      <el-table-column
        label="学员情况"
        align="center"
        prop="studentDescription"
      />
      <el-table-column
        label="授课频率"
        align="center"
        prop="frequency"
      />
      <el-table-column
        label="授课时间"
        align="center"
        prop="timeSlots"
        width="130"
      >
        <template slot-scope="scope">
          <el-tag
            v-for="item in JSON.parse(scope.row.timeSlots) "
            style="margin: 0 4px;"
            :key="item"
            size="small"
            class="mr-2"
          >{{ item }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="老师性别"
        align="center"
        prop="teacherGender"
      />
      <el-table-column
        label="课酬"
        align="center"
        prop="courseFee"
      />
      <el-table-column
        label="备注"
        align="center"
        width="130"
        prop="remarks"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="status"
      />
      <el-table-column
        label="提交时间"
        align="center"
        prop="createdAt"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="120"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-if="!isTutorrequest"
          >聊天</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="isTutorrequest"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            style="color: red;"
            @click="handleDelete(scope.row)"
            v-if="isTutorrequest"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改家教申请综合对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        :inline="true"
      >
        <el-form-item
          label="姓名"
          prop="useName"
        >
          <el-input
            v-model="form.useName"
            placeholder="请输入姓名"
          />
        </el-form-item>
        <el-form-item
          label="手机号"
          prop="phone"
        >
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item
          label="性别"
          prop="studentGender"
        >
          <el-select
            v-model="form.studentGender"
            placeholder="请选择性别"
            clearable
          >
            <el-option
              label="男"
              value="男"
            ></el-option>
            <el-option
              label="女"
              value="女"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="年级"
          prop="grade"
        >
          <el-select
            v-model="form.grade"
            placeholder="请选择年级"
            clearable
          >
            <el-option
              v-for="grade in grades"
              :key="grade"
              :label="grade"
              :value="grade"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="省"
          prop="province"
        >
          <!-- <el-input v-model="form.province" placeholder="请输入省" /> -->
          <el-select
            v-model="form.province"
            placeholder="请输入省"
            clearable
            @change="provinceChange"
          >
            <el-option
              v-for="item in province"
              :key="item.Id"
              :label="item.Name"
              :value="item.Name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="市"
          prop="city"
        >
          <!-- cityChange -->
          <!-- <el-input v-model="form.city" placeholder="请输入市" /> -->
          <el-select
            v-model="form.city"
            placeholder="请输入市"
            clearable
            @change="cityChange"
          >
            <el-option
              v-for="item in city"
              :key="item.Id"
              :label="item.Name"
              :value="item.Name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="区/县"
          prop="district"
        >
          <!-- <el-input v-model="form.district" placeholder="请输入区/县" /> -->
          <el-select
            v-model="form.district"
            placeholder="请输入区/县"
            clearable
          >
            <el-option
              v-for="item in district"
              :key="item.Id"
              :label="item.Name"
              :value="item.Name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="详细地址"
          prop="address"
        >
          <el-input
            v-model="form.address"
            placeholder="请输入详细地址"
          />
        </el-form-item>
        <el-form-item
          label="门牌号"
          prop="doorNumber"
        >
          <el-input
            v-model="form.doorNumber"
            placeholder="请输入门牌号"
          />
        </el-form-item>
        <!-- <el-form-item label="科目阶段" prop="subjectStage">
          <el-input v-model="form.subjectStage" placeholder="请输入科目阶段" />
        </el-form-item> -->
        <el-form-item
          label="科目名称"
          prop="subject"
        >
          <el-select
            v-model="form.subject"
            placeholder="科目名称"
            clearable
            filterable
            allow-create
          >
            <el-option
              v-for="item in subjects"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="学员情况"
          prop="studentDescription"
        >
          <el-input
            v-model="form.studentDescription"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          label="授课频率"
          prop="frequency"
        >
          <!-- frequencies -->
          <!-- <el-input v-model="form.frequency" placeholder="请输入授课频率" /> -->
          <el-select
            v-model="form.frequency"
            placeholder="授课频率"
            clearable
            filterable
            allow-create
          >
            <el-option
              v-for="item in frequencies"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="授课时间"
          prop="timeSlots"
        >
          <el-select
            v-model="form.timeSlots"
            placeholder="授课时间"
            clearable
            filterable
            allow-create
            multiple
            collapse-tags
          >
            <el-option
              v-for="item in timeSlots"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="教师性别"
          prop="teacherGender"
        >
          <el-select
            v-model="form.teacherGender"
            placeholder="教师性别"
            clearable
          >
            <!--'男','女','均可'  -->
            <el-option
              label="男"
              value="男"
            ></el-option>
            <el-option
              label="女"
              value="女"
            ></el-option>
            <el-option
              label="均可"
              value="均可"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="课酬"
          prop="courseFee"
        >
          <el-input
            v-model="form.courseFee"
            type="number"
            placeholder="请输入课酬"
          />
        </el-form-item>
        <el-form-item
          label="备注"
          prop="remarks"
        >
          <el-input
            v-model="form.remarks"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <!-- <el-form-item label="提交时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择提交时间">
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTutorrequest,
  getTutorrequest,
  delTutorrequest,
  addTutorrequest,
  updateTutorrequest,
  listArea,
} from '@/api/system/tutorrequest'
import dayjs from 'dayjs'
export default {
  name: 'Tutorrequest',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家教申请综合表格数据
      tutorrequestList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      province: [],
      city: [],
      district: [],
      isTutorrequest: 1,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        useName: null,
        phone: null,
        studentGender: null,
        grade: null,
        province: null,
        city: null,
        district: null,
        address: null,
        doorNumber: null,
        subjectStage: null,
        subject: null,
        studentDescription: null,
        frequency: null,
        timeSlots: [],
        teacherGender: null,
        courseFee: null,
        remarks: null,
        status: null,
        createdAt: null,
      },
      subjects: [
        '数学',
        '语文',
        '英语',
        '物理',
        '化学',
        '生物',
        '地理',
        '历史',
        '政治',
        '音乐',
        '美术',
        '体育',
        '计算机',
        '心理学',
        '哲学',
        '经济学',
        '法学',
        '医学',
        '工程学',
        '建筑学',
        '艺术设计',
        '商务英语',
        '法语',
        '西班牙语',
        '日语',
        '韩语',
        '会计学',
        '财务管理',
        '市场营销',
        '广告学',
        '新闻学',
        '社会学',
        '国际关系',
        '政治学',
        '文学',
        '翻译学',
        '语言学',
        '教育学',
        '公共管理',
        '管理学',
        '统计学',
      ],
      frequencies: [
        '一周1次',
        '一周2次',
        '每周3次',
        '每两周1次',
        '每月1次',
        '每天1次',
        '每天2次',
        '每周5次',
        '不定期',
      ],
      timeSlots: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
      grades: [
        '小学一年级',
        '小学二年级',
        '小学三年级',
        '小学四年级',
        '小学五年级',
        '小学六年级',
        '初中一年级',
        '初中二年级',
        '初中三年级',
        '高中一年级',
        '高中二年级',
        '高中三年级',
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: '用户ID不能为空', trigger: 'blur' },
        ],
        useName: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        phone: [{ required: true, message: '手机号不能为空', trigger: 'blur' }],
        grade: [{ required: true, message: '年级不能为空', trigger: 'blur' }],
        province: [{ required: true, message: '省不能为空', trigger: 'blur' }],
        city: [{ required: true, message: '市不能为空', trigger: 'blur' }],
        district: [
          { required: true, message: '区/县不能为空', trigger: 'blur' },
        ],
        address: [
          { required: true, message: '详细地址不能为空', trigger: 'blur' },
        ],
        frequency: [
          { required: true, message: '授课频率不能为空', trigger: 'blur' },
        ],
        courseFee: [
          { required: true, message: '课酬不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  async created() {
    let res = await listArea({ level: 0 })
    this.province = res.rows
    this.isTutorrequest = this.$store.getters.user.userType == 1 ? false : true
    this.queryParams.userId = this.isTutorrequest
      ? this.$store.state.user.id
      : null
    this.getList()
  },
  methods: {
    handleView(row) {
      let chats = this.$store.getters.chats
      let chat = chats.find((v) => {
        return v.user1Id === row.userId || v.user2Id === row.userId
      })
      if (chat) {
        this.$router.push({
          path: '/teacher/websocket',
          query: {
            chatId: chat.chatId,
          },
        })
        return
      }
      let chatObj = {
        name: 'chat',
        user1Id: this.$store.state.user.id,
        user2Id: row.userId,
        createdAt: dayjs().format('YYYY-MM-DD HH:mm:ss'),
      }
      this.$websocket.sendWebsocket(chatObj)
      this.$router.push({
        path: '/teacher/websocket',
        query: {},
      })
    },
    async provinceChange() {
      let data = this.province.find((v) => v.Name === this.form.province)
      let res = await listArea({ parentId: data.Id })
      this.city = res.rows
      this.$set(this.form, 'city', null)
      this.$set(this.form, 'district', null)
    },
    async cityChange() {
      let data = this.city.find((v) => v.Name === this.form.city)
      let res = await listArea({ parentId: data.Id })
      this.district = res.rows
      this.$set(this.form, 'district', null)
    },
    /** 查询家教申请综合列表 */
    getList() {
      this.loading = true
      listTutorrequest(this.queryParams).then((response) => {
        this.tutorrequestList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: this.$store.getters.id,
        useName: this.$store.getters.user.nickName || this.$store.getters.name,
        phone: this.$store.getters.user.phonenumber,
        studentGender: '女',
        grade: null,
        province: null,
        city: null,
        district: null,
        address: null,
        doorNumber: null,
        subjectStage: null,
        subject: null,
        studentDescription: null,
        frequency: null,
        timeSlots: null,
        teacherGender: null,
        courseFee: null,
        remarks: null,
        status: null,
        createdAt: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    isSelectable(row) {
      return row.status == '未完成'
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (selection && selection.length) {
        this.$emit('getItem', selection[0])
      }
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加家教申请综合'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTutorrequest(id).then((response) => {
        response.data.timeSlots = JSON.parse(response.data.timeSlots)
        this.form = response.data
        this.open = true
        this.title = '修改家教申请综合'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let form = JSON.parse(JSON.stringify(this.form))
          form.timeSlots = JSON.stringify(this.form.timeSlots)
          if (this.form.id != null) {
            updateTutorrequest(form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTutorrequest(form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除家教申请综合编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTutorrequest(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/tutorrequest/export',
        {
          ...this.queryParams,
        },
        `tutorrequest_${new Date().getTime()}.xlsx`
      )
    },
  },
}
</script>
